# 使用关键字 def 来告诉 Python 你要定义一个函数
# username为形式参数
# "刘为中"为实参


def greet_user(username):
    """显示简单的问候语"""
    print("Hello," + username.title() + "!")


# 调用函数
greet_user("刘为中")


# 可以为函数里面的参数添加默认值
def describe_pet(pet_name, animal_type='dog'):
    """ 显示宠物的信息 """
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


describe_pet(pet_name='willie')


# 为函数添加返回值
def get_formatted_name(first_name, last_name):
    """ 返回整洁的姓名 """
    full_name = first_name + ' ' + last_name
    return full_name.title()


musician = get_formatted_name('刘', "为中")
print(musician)


# 返回字典
def build_person(first_name, last_name):
    """ 返回一个字典，其中包含有关一个人的信息 """
    person = {'first': first_name, 'last': last_name}
    return person


musician = build_person('jimi', 'hendrix')
print(musician)


# 函数结合while循环
def get_formatted_name(first_name, last_name):
    """ 返回整洁的姓名 """
    full_name = first_name + ' ' + last_name
    return full_name.title()


#  这是一个无限循环 !
# while True:
#     print("\nPlease tell me your name:")
#     f_name = input("First name: ")
#     l_name = input("Last name: ")
#     # 调用函数返回姓名
#     formatted_name = get_formatted_name(f_name, l_name)
#     print("\nHello, " + formatted_name + "!")

# 传递列表
def greet_users(names):
    """ 向列表中的每位用户都发出简单的问候 """
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)


usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)


# 传递任意数量的实参
# 使用*toppings来收集任意数量的实参
def make_pizza(*toppings):
    """ 打印顾客点的所有配料 """
    print(toppings)


make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')


# 使用任意数量的关键字实参
# 形参 **user_info 中的两个星号让 Python 创建一个名为 user_info 的空字典
def build_profile(first, last, **user_info):
    """ 创建一个字典，其中包含我们知道的有关用户的一切 """
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
        profile[key] = value
    return profile


user_profile = build_profile('albert', 'einstein',
                             location='princeton',
                             field='physics')
print(user_profile)

# 将函数存储在模块中
# 导入整个模块(使用as给模块指定别名)
import Module as md
# 通过模块的名称调用模块内的方法
md.make_pizza(16, 'pepperoni')
md.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

# 导入特定的函数
# 使用as给函数指定别名
from Module import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')

# 导入模块中的所有函数
from Module import *
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
